//
//  ViewController.m
//  BLE4.0Example
//
//  Created by zxy on 2017/9/27.
//  Copyright © 2017年 saint. All rights reserved.
//

#import "ViewController.h"
#import "YHBLECentralManager.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *hud;
@property (weak, nonatomic) IBOutlet UITextField *wifiName;
@property (weak, nonatomic) IBOutlet UITextField *wifiPasswd;
@property (weak, nonatomic) IBOutlet UISegmentedControl *wfiiType;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData:) name:YH_BLE_SCANFSTATE object:nil];
    self.hud.hidesWhenStopped = YES;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)scanf:(id)sender {
    [[YHBLECentralManager shared] startScanWith:10];
}
- (IBAction)stop:(id)sender {
    [[YHBLECentralManager shared] stopScan];
}

- (void)reloadData:(NSNotification *)noti{
    
    if (![noti.object boolValue]) {
        NSMutableString *st = [[NSMutableString alloc]init];
        for (CBPeripheral *item  in [YHBLECentralManager shared].peripherals) {
            [st appendFormat:@"%@\n",item.name];
        }
        
        dispatch_async(dispatch_get_main_queue(), ^{
      [self.tableView reloadData];
            [self.hud stopAnimating];
        });
    }else{
        dispatch_async(dispatch_get_main_queue(), ^{
            
            [self.hud startAnimating];
        });
    }
}
- (IBAction)sendMsg:(id)sender {
    Byte headerbyte[] = {0xa5,0x5a,0x00,0x01,0xc0,0x00,0x43,0xC0,0x11};
    NSData *headData = [NSData dataWithBytes:headerbyte length:9];
    
    
    NSData *nameData = [self.wifiName.text dataUsingEncoding:NSUTF8StringEncoding];
    nameData = [self fix32with:nameData];
    
    NSData *passWd = [self.wifiPasswd.text dataUsingEncoding:NSUTF8StringEncoding];
    passWd = [self fix32with:passWd];
    Byte type[] = {0x00,0x01,0x02};
    NSData *typeData = [NSData dataWithBytes:&type[self.wfiiType.selectedSegmentIndex] length:1];
   
    

    
    NSMutableData *pkgData = [[NSMutableData alloc]init];;
    [pkgData appendData:headData];
    [pkgData appendData:[NSData dataWithBytes:(Byte[]){0x20} length:1]];
    [pkgData appendData:nameData];
    [pkgData appendData:[NSData dataWithBytes:(Byte[]){0x20} length:1]];
    [pkgData appendData:passWd];
    [pkgData appendData:typeData];
    [pkgData appendData:[NSData dataWithBytes:(Byte[]){0x66} length:1]];
    
    [[YHBLECentralManager shared] sendMessage:pkgData];
}




- (NSData *)fix32with:(NSData *)data{
    if (data.length != 32) {
        int fixLenght = (int)(32 - data.length);
        NSMutableData *fixData = [[NSMutableData alloc]initWithLength:fixLenght];
        [fixData appendData:data];
        return fixData;
    }
    return data;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [YHBLECentralManager shared].peripherals.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CBPeripheral *peripheral = [YHBLECentralManager shared].peripherals[indexPath.row];
    cell.textLabel.text = peripheral.name.length > 0 ? peripheral.name : @"未知设备";
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CBPeripheral *peripheral = [YHBLECentralManager shared].peripherals[indexPath.row];
    [[YHBLECentralManager shared] connectTo:peripheral];
}

@end
